package com.hualude.roadsign.service.impl;

import com.hualude.roadsign.dto.*;
import com.hualude.roadsign.repository.*;
import com.hualude.roadsign.service.GuardrailFaultService;
import com.hualude.roadsign.utils.PushUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by pjn on 2017/8/14.
 */
@Service
public class GuardrailFaultServiceImpl implements GuardrailFaultService {
    @Resource
    private GuardrailFaultRepository guardrailFaultRepository;
    @Resource
    private GuardrailRepository guardrailRepository;
    @Resource
    private UserRepository userRepository;
    @Resource
    private OrgRepository orgRepository;
    @Resource
    private PushHistoryRepository pushHistoryRepository;

    private String strUserIds;

    private SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public DTResponseDto getGuardrailReportFault4DT(DTRequestDto dtRequestDto, Integer actorId, Integer userId, Integer orgId) throws Exception {
        Integer count;
        List<DTGuardrailFaultDto> dto4DTs;
        count = guardrailFaultRepository.getGuardrailReportFaultCount4DT(actorId, userId, orgId, dtRequestDto.getSearch());
        dto4DTs = guardrailFaultRepository.getGuardrailReportFault4DT(dtRequestDto, actorId, userId, orgId);
        for (DTGuardrailFaultDto dto : dto4DTs) {
            dto.setGuardrailCheckerName(userRepository.getUserNameByUserId(dto.getGuardrailCheckerId()));
        }
        DTResponseDto dtResponseDto = new DTResponseDto(dtRequestDto.getDraw(), count, count, dto4DTs);
        return dtResponseDto;
    }

    @Transactional
    public Integer updateGuardrailFault(Integer guardrailFaultId, Integer guardrailFaultLevel, Integer guardrailFaultType, String guardrailFaultPhoto, String guardrailFaultReason, Double guardrailMoney) throws Exception {
        return guardrailFaultRepository.updateGuardrailFault(guardrailFaultId, guardrailFaultLevel, guardrailFaultType, guardrailFaultPhoto, guardrailFaultReason, guardrailMoney);
    }

    @Transactional
    public Integer deleteGuardrailFaultByIds(String guardrailFaultIds) throws Exception {
        String[] faultIdArray = guardrailFaultIds.split(",");
        int i = 0;
        for (i = 0; i < faultIdArray.length; i++) {
            guardrailFaultRepository.deleteGuardrailFaultByIds(Integer.parseInt(faultIdArray[i]));
        }
        return i;
    }


    public Integer reportGuardrailFault(final Integer guardrailId, Integer guardrailFaultCreatorId, Integer guardrailFaultLevel, Integer guardrailFaultType,
                                        String guardrailFaultPhoto, String guardrailFaultReason, Double guardrailMoney, final Integer guardrailOrgId, Integer guardrailMainOrgId) throws Exception {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        if (guardrailMoney == null)
            guardrailMoney = 0.0;
        GuardrailFaultDto insertDto = new GuardrailFaultDto();
        insertDto.setGuardrailId(guardrailId);
        insertDto.setGuardrailFaultCreatorId(guardrailFaultCreatorId);
        insertDto.setGuardrailFaultLevel(guardrailFaultLevel);
        insertDto.setGuardrailFaultType(guardrailFaultType);
        insertDto.setGuardrailFaultTime(df.format(new Date()));
        insertDto.setGuardrailFaultPhoto(guardrailFaultPhoto);
        insertDto.setGuardrailFaultReason(guardrailFaultReason);
        insertDto.setGuardrailMoney(guardrailMoney);
        insertDto.setGuardrailOrgId(guardrailOrgId);
        insertDto.setGuardrailMainOrgId(guardrailMainOrgId);

        List<UserDto> userDtos = userRepository.getUserIdsByOrgIdAndActorId(guardrailOrgId, 3);
        final String msg = "护栏 NO：" + guardrailId + " 待审核";
        strUserIds = "";
        for (UserDto userDto : userDtos) {
            strUserIds += userDto.getUserId().toString() + ",";
        }
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    if (!strUserIds.equals("")) {
                        strUserIds = strUserIds.substring(0, strUserIds.length() - 1);
                        PushUtil.sendPushToUser(strUserIds, msg);
                        PushHistoryDto pushHistoryDto = new PushHistoryDto(guardrailOrgId, 3, msg, formatter.format(new Date()),7);
                        pushHistoryRepository.save(pushHistoryDto.toBean());
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();

        return guardrailFaultRepository.save(insertDto.toBean()).getGuardrailFaultId();
    }

    public Integer getGuardrailFaultIsReport(Integer guardrailId) throws Exception {
        List<GuardrailFaultDto> guardrailFaultDtos = guardrailFaultRepository.getGuardrailFaultIsReport(guardrailId);
        Integer result = 1;
        if (guardrailFaultDtos.size() != 0) {
            Integer mainCheck = guardrailFaultDtos.get(0).getGuardrailMainCheck();
            Integer check = guardrailFaultDtos.get(0).getGuardrailCheck();
            Integer status = guardrailFaultDtos.get(0).getGuardrailMaintenanceStatus();
            if (status == 2) {
                result = 1;//维修完成
            } else if (mainCheck == -1 || check == -1) {
                result = 1;//
            } else {
                result = 0;
            }
        }
        return result;
    }

    public DTResponseDto getGuardrailFaultReport4DT(DTRequestDto dtRequestDto, Integer actorId, Integer orgId, String roadName, String startRoad, String endRoad) throws Exception {
        if (roadName.equals("") && startRoad.equals("") && endRoad.equals("")) {
            return new DTResponseDto(dtRequestDto.getDraw(), 0, 0, "");
        } else {
            List<DTGuardrailFaultDto> dto4DTs = guardrailRepository.getGuardrailFaultReport4DT(dtRequestDto, actorId, orgId, roadName, startRoad, endRoad);
            for (DTGuardrailFaultDto dto : dto4DTs) {
                List<GuardrailFaultDto> guardrailFaultDtos = guardrailFaultRepository.getGuardrailFaultIsReport(dto.getGuardrailId());
                Integer result = 1;
                if (guardrailFaultDtos.size() != 0) {
                    Integer mainCheck = guardrailFaultDtos.get(0).getGuardrailMainCheck();
                    Integer check = guardrailFaultDtos.get(0).getGuardrailCheck();
                    Integer status = guardrailFaultDtos.get(0).getGuardrailMaintenanceStatus();
                    if (status == 2) {
                        result = 1;//维修完成
                    } else if (mainCheck == -1 || check == -1) {
                        result = 1;//
                    } else {
                        result = 0;
                    }
                }
                dto.setReportStatus(result);
            }
            DTResponseDto dtResponseDto = new DTResponseDto(dtRequestDto.getDraw(), 1, 1, dto4DTs);
            return dtResponseDto;
        }
    }

    public List<String> getStartRoad(String roadName, Integer actorId, Integer orgId) throws Exception {
        return guardrailRepository.getRoadURoadName(roadName, actorId, orgId);
    }

    public List<String> getSectionByStartRoad(String roadName, Integer actorId, Integer orgId) throws Exception {
        return guardrailRepository.getSectionUStartRoad(roadName, actorId, orgId);
    }

    @Override
    public List<GuardrailFaultDto> getGuardrailFaultList4App(Integer orgId, String actorIds, Integer faultStatus, Integer count, Integer size) throws Exception {
        return guardrailFaultRepository.getGuardrailFaultList4App(orgId, actorIds, faultStatus, count, size);
    }

    @Override
    public List<GuardrailDto> getGuardrailCoordinates4Map(Integer orgId, Integer orgType, Integer actorId) throws Exception {
        return guardrailFaultRepository.getGuardrailCoordinates4Map(orgId, orgType, actorId);
    }
}
